<?php

    /**
    * Main class for working with cms settings.
    * @author NU SIT Bar
    * @version 1.0
    * @created 17.Jul.2012 17:39:44 h
    */

    class Settings extends MX_Controller 
    {

        private $data;

        function __construct()
        {
            parent::__construct();


			require_once("../urlify.php");
            $this->load->model('settings_model');
            //$this->load->model('pages_model','posts_model');
			$this->load->helper('sitemap');
            $this->load->library('translate', array('lang' => $this->session->userdata('my_language')));
            $this->load->config('pages');
        }

        function index() { show_404(); }

        function view_edit_thimbnail_size($params){

            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Edit ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = 'EDIT'; 
            $this->data['post'] = $this->settings_model->view_edit_thimbnail_size($_GET['post_ID']);
            echo $this->load->view('view_edit_thumbnail_size', $this->data, TRUE);

        }

        function view_add_thimbnail_size($params){

            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Add ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = 'NEW'; 
            echo $this->load->view('view_edit_thumbnail_size', $this->data, TRUE);

        }

        function view_all_thimbnail_size($params){

            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "View All ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['all_posts'] = $this->settings_model->view_all_thumbnail_size(); 
            echo $this->load->view('view_all_thumbnail_size', $this->data, TRUE); 

        }

        function view_all_words($params){

            $this->load->helper('clean_html');
            
            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "View All ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['all_words'] = $this->translate->trans_words_array($this->settings_model->view_all_words()); 
            echo $this->load->view('view_all_words', $this->data, TRUE); 

        }

        function view_edit_word($params){ 

            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Edit ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = 'EDIT'; 
            $this->data['word'] = $this->settings_model->view_edit_word($_GET['post_ID']);
            echo $this->load->view('edit_word', $this->data, TRUE);

        }
        
        function view_new_word($params){ 

            $this->data['link'] = $params['link'];
            $this->data['page'] = ucfirst($_GET['post_type']);
            $this->data['title'] = "Add ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = 'NEW'; 
            echo $this->load->view('edit_word', $this->data, TRUE);

        }

        function submit_post_thumbnails(){

            switch($_POST['post_action']){

                case 'NEW':     $this->settings_model->create_thumbnail_size(); $message = 'Successful new operation';
                    break;

                case 'EDIT':    $this->settings_model->update_thumbnail_size(); $message = 'Successful edit operation';
                    break;

            } 

            return TRUE.'-'.$message;

        }

        function submit_post_word(){

            switch($_POST['post_action']){

                case 'NEW':     $this->settings_model->create_word();   $message = 'Successful new operation';
                    break;

                case 'EDIT':    $this->settings_model->update_word();   $message = 'Successful edit operation';
                    break;

            }  

            return TRUE.'-'.$message;

        }

        function delete_thumbnail(){

            $this->settings_model->delete_thumbnail($_GET['post_ID']);

            return TRUE;

        }

        function delete_word($params){
            
            $this->settings_model->delete_word($params['ID']);
            return TRUE;

        }

        function change_lang(){

            /*$this->lang_ses->setLang($_POST['lang']);
            echo json_encode(true);  */

        }

		function sitemap($params){
            $this->data['link'] = $params['link'];
            $this->data['page'] = '';//ucfirst($_GET['post_type']);
            $this->data['title'] = "Recreate ".$_GET['post_type'];
            $this->data['post_type'] = $_GET['post_type']; 
            $this->data['post_action'] = 'EDIT'; 
            echo $this->load->view('sitemap', $this->data, TRUE);

        }
		
		function recreate_sitemap() {	
			$result = true;
			$this->_ci = get_instance();

			$this->load->model('cms_pro','cms');
			$sitemap = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$this->load->library('rest', array(
				 'server' => REST_URL_HOTELS
			 ));

			$langs = array('en', 'de', 'ru', 'me');
			 foreach($langs as $lang) :
				//load hotels for listing
				$this->rest->initialize(array(
					 'server' => REST_URL_HOTELS,
					'http_user' => 'cipa',
					'http_pass' => 'booking',
					'http_auth' => 'basic' // or 'digest'
				 ));
				 $params=array(
					 "dest_hot" => '',
					 "lang" => $lang,
				);
				$hotels = $this->rest->post("searchPreview", $params);
				
				$sitemap .= create_link_sitemap_block($lang, 'hotels');
				$sitemap .= create_link_sitemap_block($lang, 'hotels/search?dest_hot=&amp;non_specific_dates=1');				
				foreach($hotels as $hotel) :
					$sitemap .= create_link_sitemap_block($lang, 'hotels/details_preview?hotel_id='.$hotel->object_ID.'');
				endforeach;	
				$sitemap .= create_link_sitemap_block($lang, 'faq_hotels');
				
				//load destinations for listing
				$allowedDestinations = array(
					"2" => "Budva",
					"3" => "Herceg Novi",
					"4" => "Kotor",
					"5" => "Tivat",
					"6" => "Ulcinj",
					"14" => "Sv. Stefan",
					"15" => "Petrovac",
					"45" => "Ada Bojana",
					"53" => "Dubrovnik",
					"1" => "Bar",
					"54" => "Ostrog Monastery",
					"55" => "Budva Old Town",
					"48" => "Albania",
				);
				$destinations = $this->rest->post ( 'destinationList', array ('lang' => $lang) );
				$filtered = array();
				foreach($destinations as $destination) {
					if(isset($allowedDestinations[(string)$destination->ID])) {
						$filtered[] = $destination;
					}
				}			

				$destinations = $filtered;
				$sitemap .= create_link_sitemap_block($lang, 'destinations');
				foreach($destinations as $destination) :
					$sitemap .= create_link_sitemap_block($lang, 'destinations/'.urlify::filter($destination->name).'-'.$destination->ID);
				endforeach;	
				
				//load excursions for listing			
				$this->rest->initialize(array(
					'server' => REST_URL_EXCURSIONS
				 ));
				 $params = array(
					 'eb_starting' => 'Any',
					 'eb_freetext' => '',
					 'exc_sort'    => 'none',
					 'lang'        => $lang
				);

				$excursions = $this->rest->post('excursions', $params, 'json');
				$sitemap .= create_link_sitemap_block($lang, 'excursions');
				$sitemap .= create_link_sitemap_block($lang, 'excursions/search');				
				foreach($excursions as $excursion) :
					$sitemap .= create_link_sitemap_block($lang, 'excursions/'.urlify::filter($excursion->title).'-'.$excursion->id);
				endforeach;	
				$sitemap .= create_link_sitemap_block($lang, 'faq_excursions');
				
				//load rentacar for listing
				 $this->rest->initialize(array(
					 'server' => REST_URL_RENTACAR
				 ));
				$params = array('lang'=>$lang);            
				
				$rentacars = $this->rest->get('all_cars', $params, 'json');
				$sitemap .= create_link_sitemap_block($lang, 'rentacar/home');
				$sitemap .= create_link_sitemap_block($lang, 'rentacar/browse_fleet');				
				foreach($rentacars as $car) :
					$sitemap .= create_link_sitemap_block($lang, 'rentacar/'.urlify::filter($car->name).'-'.$car->id);
				endforeach;	
				$sitemap .= create_link_sitemap_block($lang, 'faq_rentacar');
				
				
				//load news for listing				
				$news=$this->cms->get_news();
				$news = $this->translate->trans_res_array($news);
				$sitemap .= create_link_sitemap_block($lang, 'news');				
				foreach($news as $n) :
					$sitemap .= create_link_sitemap_block($lang, 'news/'.urlify::filter($n['post_title']).'-'.$n['post_ID']);
				endforeach;	


				//other pages
				$sitemap .= create_link_sitemap_block($lang, 'about');
				$sitemap .= create_link_sitemap_block($lang, 'contact');
				$sitemap .= create_link_sitemap_block($lang, 'sitemap');
				$sitemap .= create_link_sitemap_block($lang, 'terms_conditions');
				$sitemap .= create_link_sitemap_block($lang, 'polices');
				$sitemap .= create_link_sitemap_block($lang, 'legal_mentions');

			endforeach;
			
			$sitemap .= '</urlset>';
			
			$path = str_replace("//", "/", $_SERVER['DOCUMENT_ROOT']."/assets/sitemap.xml");
			file_put_contents($path, $sitemap);
			
			//$url = "www.google.com/webmasters/tools/ping?sitemap=http%3A%2F%2Fwww.globtour.me%2Fassets%2Fsitemap.xml";
			$this->rest->initialize(array(
				'server' => 'www.google.com/webmasters/tools/'
			));
			$params = array( 
				'sitemap' => 'http%3A%2F%2Fwww.globtour.me%2Fassets%2Fsitemap.xml'
			);
			$response = $this->rest->get('ping', $params);
			
			return array('result' => $result, 'msg'=> '');
		}
}